package Classes.GameDatas// ActionScript file
{
	import flash.geom.Point;

	/**
	 *建设类型 
	 * @author upstream
	 * 
	 */	
	public class BuildType
	{
		public static const BUY:String="buy";/*购买的，从商店购买建筑并放置与地图上*/
		public static const PLACE:String="place";/*放置的，从仓库中将建筑物放置到地图上*/
		public static const MOVE:String="move";/*移动的，在地图上移动了建筑物*/
		public static const DELETE:String="delete";/*删除的，从地图上删除的建筑*/
		public static const INVALID:String="invalid";/*无效的，购买后取消、放置后取消、移动后放置到初始位置上都视为无效*/
		private var _changeType:String=INVALID;
		private var _building:GD_Building_Func;
		private var _initGrid:GD_MapGrid;/*改变前的建筑物地图格*/
		public function BuildType(building:GD_Building_Func):void
		{
			this._building=building;
			this.init();
		}
		
		private function init():void
		{
			this._initGrid=this.building.view.mapGrid;
		}
		
		public static function creat(creatObj:Object):BuildType
		{
			var buildType:BuildType=new BuildType(creatObj.building);
			return buildType;
		}
		
		public function setType(type:String):void
		{
			switch(type)
			{
				case BUY:
					this.setBuy();
					break;
				case PLACE:
					this.setPlace();
					break;
				case MOVE:
					this.setMove();
					break;
				case DELETE:
					this.setDelete();
					break;
				case INVALID:
					this.setInvalid();
					break;
			}
		}
		
		private function setBuy():void
		{
			this._changeType=BUY;
		}
		
		private function setPlace():void
		{
			if(this.initGrid)//存在初始地图格记录，将改变类型置换为移动
			{
				this._changeType=MOVE;
			}
			else
			{
				this._changeType=PLACE;
			}
		}
		
		private function setMove():void
		{
			if(this.initGrid)//存在初始地图格记录时，如果记录的地图格与当前地图格相同，置为无效，不相同，置为移动;否则，不改变状态
			{
				if(this.equalGrid())
				{
					this._changeType=INVALID;
				}
				else
				{
					this._changeType=MOVE;
				}
			}
		}
		
		private function setDelete():void
		{
			if(this.changeType==BUY || this.changeType==PLACE)//之前变化类型如果为"放置","购买",将清除之前的变化操作，否则将类型变为"删除"
			{
				this.building.recoverChange();
				this._changeType=INVALID;
			}
			else
			{
				this.building.recoverPlace();
				this._changeType=DELETE;
			}
		}
		
		private function setInvalid():void
		{
			this.building.recoverChange();
			this._changeType=INVALID;
		}
		
		private function equalGrid():Boolean
		{
			if(!this.building.view.mapGrid)return false;
			return this.building.view.mapGrid.coordinatePoint.equals(this.initGrid.coordinatePoint);
		}

		/*getter & setter*/
		public function get changeType():String
		{
			return _changeType;
		}

		public function get building():GD_Building_Func
		{
			return _building;
		}

		public function get initGrid():GD_MapGrid
		{
			return _initGrid;
		}


	}
}